<template>
  <div class="postdetail">
    <div class="normalPost" v-if="postInfo.type == 1">
      <div class="header">
        <div class="iconfont iconjiantou2" @click="$router.back()"></div>
        <div class="iconfont iconnew"></div>
        <div
          @click="handleFollow"
          class="followBtn"
          :class="{
            unfollow: !postInfo.has_follow,
          }"
        >
          {{ postInfo.has_follow === true ? "已关注" : "关注" }}
        </div>
      </div>
      <div class="maincontent">
        <div class="title">{{ postInfo.title }}</div>
        <div class="info">
          {{ postInfo.user.nickname || postInfo.user.username }}
          <span class="date">2019-10-10</span>
        </div>
        <div class="content" v-html="postInfo.content"></div>
      </div>
    </div>
    <div class="videoPost" v-if="postInfo.type == 2">
      <div class="videoCover" @click="videoPlay">
        <video
          ref="btnPlay"
          poster="http://157.122.54.189:9083/uploads/image/IMG1606459976647.jpeg"
          :src="postInfo.content"
          @play="toPlay"
          @pause="toPause"
        ></video>
        <div class="iconfont iconshipin"></div>
      </div>
      <div class="info">
        <img src="@/assets/default.png" alt="" class="avatar" />
        <div class="name">
          {{ postInfo.user.nickname || postInfo.user.username }}
        </div>
        <div class="followBtn">关注</div>
      </div>
      <div class="content">{{ postInfo.title }}</div>
    </div>
    <div class="btns">
      <div
        @click="handleLike"
        class="btn"
        :class="{
          hasLike: postInfo.has_like,
        }"
      >
        <span class="iconfont icondianzan"></span>
        {{ postInfo.like_length }}
      </div>
      <div class="btn">
        <span class="iconfont iconweixin"></span>
        微信
      </div>
    </div>

    <h2 class="commentTitle">精彩跟帖</h2>
    <CommentMain
      :commentData="comment"
      v-for="(comment, index) in commentList"
      :key="index"
    />

    <div
      class="commentMore"
      @click="$router.push('/MoreComment/' + $route.params.id)"
    >
      更多跟帖
    </div>
    <CommentInput @reloadComment="reloadComment" />
  </div>
</template>

<script>
import CommentMain from "../components/Comment/Main";
import CommentInput from "../components/Comment/Input";
export default {
  components: {
    CommentMain,
    CommentInput,
  },
  data() {
    return {
      postInfo: {},
      commentList: {},
    };
  },
  created() {
    this.$axios({
      url: "/post/" + this.$route.params.id,
    }).then((res) => {
      // console.log(res.data);
      this.postInfo = res.data.data;
    });
    this.reloadComment();
  },
  methods: {
    reloadComment() {
      this.$axios({
        url: "/post_comment/" + this.$route.params.id,
      }).then((res) => {
        if (res.data.data.length > 3) {
          res.data.data.length = 3;
        }
        this.commentList = res.data.data;
        console.log(this.commentList);
      });
    },
    toPlay() {
      document.querySelector(".iconshipin").style.display = "none";
      this.$refs.btnPlay.play();
    },
    toPause() {
      document.querySelector(".iconshipin").style.display = "block";
      this.$refs.btnPlay.pause();
    },
    videoPlay() {
      this.$refs.btnPlay.paused == true ? this.toPlay() : this.toPause();
    },
    handleFollow() {
      // 判断当前关注还是未关注
      if (this.postInfo.has_follow) {
        // 关注的用户ID
        this.$axios({
          url: "/user_unfollow/" + this.postInfo.user.id,
        }).then((res) => {
          // 因为这个功能按钮不是特别重要，不需要从后台重新那数据，可以直接手动改样式
          this.postInfo.has_follow = false;
        });
      } else {
        this.$axios({
          url: "/user_follows/" + this.postInfo.user.id,
        }).then((res) => {
          this.postInfo.has_follow = true;
        });
      }
    },
    handleLike() {
      this.$axios({
        url: "/post_like/" + this.$route.params.id,
      }).then((res) => {
        // console.log(res.data);
        //因为请求地址一样,通过Message来判断
        if (res.data.message == "点赞成功") {
          this.postInfo.has_like = true;
          this.postInfo.like_length++;
          this.$toast("点赞成功");
        }
        if (res.data.message == "取消成功") {
          this.postInfo.has_like = false;
          this.postInfo.like_length--;
          this.$toast("取消成功");
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.postdetail {
  padding-bottom: 50/360 * 100vw;
  .commentTitle {
    text-align: center;
    font-size: 22/360 * 100vw;
    font-weight: normal;
    padding: 20/360 * 100vw;
  }
  .commentMore {
    width: 122/360 * 100vw;
    height: 32/360 * 100vw;
    border: 1px solid #888;
    border-radius: 15/360 * 100vw;
    margin: 30/360 * 100vw auto;
    text-align: center;
    line-height: 32/360 * 100vw;
    margin-bottom: 80/360 * 100vw;
  }
  // 普通文章css
  .normalPost {
    .header {
      display: flex;
      padding: 0 20/360 * 100vw;
      align-items: center;
      .iconjiantou2 {
        font-size: 16/360 * 100vw;
      }
      .iconnew {
        flex-grow: 1;
        font-size: 66/360 * 100vw;
        padding-left: 8/360 * 100vw;
      }
      .followBtn {
        font-size: 14/360 * 100vw;
        border: 1px solid #ccc;
        height: 26/360 * 100vw;
        line-height: 26/360 * 100vw;
        padding: 0 14/360 * 100vw;
        border-radius: 13/360 * 100vw;
        &.unfollow {
          border: 1px solid #ff0000;
          background-color: #ff0000;
          color: #fff;
        }
      }
    }
    .maincontent {
      padding: 0 16/360 * 100vw;
      .title {
        font-size: 22/360 * 100vw;
        font-weight: bold;
      }
      .info {
        font-size: 12/360 * 100vw;
        color: #888;
        margin-top: 5/360 * 100vw;
        .date {
          padding-left: 12/360 * 100vw;
        }
      }
      .content {
        font-size: 16/360 * 100vw;
        color: #333;
        margin-top: 30/360 * 100vw;
        /deep/ img {
          max-width: 100%;
        }
      }
    }
  }

  // 视频文章css
  .videoPost {
    padding: 5/360 * 100vw 12/360 * 100vw;
    .info {
      display: flex;
      padding-top: 10/360 * 100vw;
      .avatar {
        width: 25/360 * 100vw;
        height: 25/360 * 100vw;
        border-radius: 50%;
        object-fit: cover;
      }
      .name {
        flex-grow: 1;
        font-size: 14/360 * 100vw;
        color: #888;
        padding-left: 8/360 * 100vw;
      }
      .followBtn {
        font-size: 12/360 * 100vw;
        border: 1px solid #ccc;
        height: 26/360 * 100vw;
        line-height: 28/360 * 100vw;
        padding: 0 16/360 * 100vw;
        border-radius: 13/360 * 100vw;
      }
    }
    .content {
      margin-top: 14/360 * 100vw;
      font-size: 20/360 * 100vw;
    }
    .videoCover {
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
      video {
        width: 100%;
      }
      .iconshipin {
        position: absolute;
        font-size: 46/360 * 100vw;
        color: #fff;
        background-color: rgba(0, 0, 0, 0.3);
        border-radius: 50%;
      }
    }
  }

  // 两个按钮css
  .btns {
    display: flex;
    justify-content: space-evenly;
    margin-top: 60/360 * 100vw;
    padding: 20/360 * 100vw;
    border-bottom: 4px solid #e4e4e4;
    .btn {
      border: 1px solid #888;
      height: 34/360 * 100vw;
      line-height: 34/360 * 100vw;
      padding: 0 16/360 * 100vw;
      border-radius: 20/360 * 100vw;
      font-size: 16/360 * 100vw;
      color: #333;
      .iconfont {
        padding-right: 5/360 * 100vw;
      }
      .iconweixin {
        color: #00c800;
      }
      &.hasLike {
        color: red;
      }
    }
  }
}
</style>